package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.RawProduct;
import com.ruoyi.purchase.service.IRawProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 冷链货品Controller
 *
 * @author kevinness
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/rawProduct")
public class RawProductController extends BaseController
{
    @Autowired
    private IRawProductService rawProductService;

    /**
     * 查询冷链货品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RawProduct rawProduct)
    {
        startPage();
        List<RawProduct> list = rawProductService.selectRawProductList(rawProduct);
        return getDataTable(list);
    }

    /**
     * 查询特定名称货品
     */
    @PostMapping("/listByName")
    public AjaxResult getByName(@RequestBody List<String> names){
        List<RawProduct> list = new ArrayList<RawProduct>();
        for(String name:names){
            RawProduct rawProduct = new RawProduct(name);
            list.addAll(rawProductService.selectRawProductList(rawProduct));
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询满足条件的供货商
     */
    @PostMapping("/suppliers")
    public AjaxResult suppliers(@RequestBody List<String> productNames){
        if (productNames == null || productNames.isEmpty()) {
            return error("请选择货品"); // 如果没有货品名称，则返回空集合
        }

        // 使用第一个货品的供货商作为初始的 commonSuppliers
        Set<String> commonSuppliers = new HashSet<>(
                rawProductService.selectRawProductByName(productNames.get(0)));

        // 遍历剩余的货品名称并更新 commonSuppliers
        for (int i = 1; i < productNames.size(); i++) {
            String product = productNames.get(i);
            Set<String> suppliersForProduct = new HashSet<>(rawProductService.selectRawProductByName(product));
            commonSuppliers.retainAll(suppliersForProduct);

            // 如果在任何一点上 commonSuppliers 变为空，我们可以提前退出
            if (commonSuppliers.isEmpty()) {
                break;
            }
        }

        if (commonSuppliers.isEmpty()) {
            return error("不存在满足条件的供货商");
        }

        return AjaxResult.success(commonSuppliers);
    }

    /**
     * 导出冷链货品列表
     */
    @Log(title = "冷链货品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RawProduct rawProduct)
    {
        List<RawProduct> list = rawProductService.selectRawProductList(rawProduct);
        ExcelUtil<RawProduct> util = new ExcelUtil<RawProduct>(RawProduct.class);
        util.exportExcel(response, list, "冷链货品数据");
    }

    /**
     * 获取冷链货品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(rawProductService.selectRawProductById(id));
    }

    /**
     * 新增冷链货品
     */
    @Log(title = "冷链货品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RawProduct rawProduct)
    {
        return toAjax(rawProductService.insertRawProduct(rawProduct));
    }

    /**
     * 修改冷链货品
     */
    @Log(title = "冷链货品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RawProduct rawProduct)
    {
        return toAjax(rawProductService.updateRawProduct(rawProduct));
    }

    /**
     * 删除冷链货品
     */
    @Log(title = "冷链货品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(rawProductService.deleteRawProductByIds(ids));
    }
}